import { request } from '@/utils/request';
import { useUserStore } from '@/store/modules/user';
import constants from '@/utils/constants';

// 获取流程控制按钮方法
export function getElementButton(params) {
  return request({
    url: '/api/task/getElementButton',
    method: 'get',
    params,
  });
}

// 获取流程审批轨迹
export function getApprovalTracks(data) {
  return request({
    url: '/api/task/getApprovalTracks',
    method: 'post',
    data,
  });
}

export function saveFormDataJson(data) {
  return request({
    url: '/api/formData/saveFormDataValue',
    method: 'post',
    data,
  });
}

export function saveProcessDraft(data) {
  return request({
    url: '/api/task/saveProcessDraft',
    method: 'post',
    data,
  });
}
// 启动/发起/提交流程
export function startProcess(data) {
  // const data={
  //   processKey:procKey,
  //   processName:procName,
  //   businessKey:businessKey
  // }
  return request({
    url: '/api/task/startProcess',
    method: 'post',
    data,
  });
}


//用户通过操作
export function userApprove(data) {    
    return request({
      url: '/api/task/userApprove',
      method: 'post',
      data,
    });   
  }

export function userDiscard(data) {
  return request({
    url: '/api/task/userDiscard',
    method: 'post',
    data,
  });
}

export function userRecall(data) {
  return request({
    url: '/api/task/userRecall',
    method: 'post',
    data,
  });
}

export function userTransfer(data) {
  return request({
    url: '/api/task/userTransfer',
    method: 'post',
    data,
  });
}

export function getRejectActivities(data) {
  return request({
    url: '/api/task/getRejectActivities',
    method: 'post',
    data,
  });
}

export function getToolbarSettings(params) {
  return request({
    url: '/api/task/getToolbarSettings',
    method: 'get',
    params,
  });
}



export function userReject(data) {
  return request({
    url: '/api/task/userReject',
    method: 'post',
    data,
  });
}

export function userSignature(data) {
  return request({
    url: '/api/task/userSupplementSignature',
    method: 'post',
    data,
  });
}

export function userCirculate(data) {
  return request({
    url: '/api/task/userCirculate',
    method: 'post',
    data,
  });
}

export function userReminder(data) {
  return request({
    url: '/api/task/userReminder',
    method: 'post',
    data,
  });
}


export function userAppend(data) {
  return request({
    url: '/api/task/userAppend',
    method: 'post',
    data,
  });
}



// 外部表单保存方法,暂不启用
export function saveSelfFormDataJson(id, form_type, form_url, mapJson, dataJson) {
  const data = {
    id,
    form_type,
    form_url,
    mapJson,
    dataJson,
  };
  return request({
    url: '/api/form/saveFormDataJson',
    method: 'post',
    data,
  });
}
export function downLoadFile(url, fileName) {
  return request({
    method: 'POST',
    url: '/api/' + constants.downFiles + '?access_token=' + useUserStore().token,
    data: {
      fileName: fileName,
      filePath: url,
    },
    responseType: 'blob',
  }).then((response) => {
    var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
    var contentDisposition = decodeURI(response.headers['content-disposition']);
    var result: any = patt.exec(contentDisposition);
    var fileName = result[1];

    let resdata = response.data;
    const blob = new Blob([resdata], {
      type: 'application/octet-stream;charset=utf-8',
    });

    // 从响应头中获取文件名称
    if ('download' in document.createElement('a')) {
      // 非IE下载
      const elink = document.createElement('a');
      elink.download = fileName;
      elink.href = URL.createObjectURL(blob);
      elink.style.display = 'none';
      document.body.appendChild(elink);
      elink.click();
      URL.revokeObjectURL(elink.href); // 释放URL 对象
      document.body.removeChild(elink);
    } else {
      // IE10+下载
    }
  });
}




// 获取流程参数变量
export function getflowVariables(formId) {
  const data = {    
    formId,    
  };
  return request({
    url: '/api/processManage/getWorkflowVariables',
    method: 'get',    
    params: data,
  });
  
}

//流程监控
// 获取轨迹图
export function getTrack(data) {
  return request({
    url: '/api/task/getTrack',
    method: 'get',
    responseType: 'blob',
    params: data,
  });
}

// 获取审核记录
export function getUserTaskTrack(data) {
  return request({
    url: '/api/task/getUserTaskTrack',
    method: 'get',    
    params: data,
  });
}

// 获取流程基本信息
export function getWorkflowBasicInfo(data) {
  return request({
    url: '/api/task/getWorkflowBasicInfo',
    method: 'get',    
    params: data,
  });
}
// 获取流程预估信息
export function getWorkflowEstimate(data) {
  return request({
    url: '/api/task/getWorkflowEstimate',
    method: 'get',    
    params: data,
  });
}
